<?php
/**
 * Synchronize Inquiries Amount to optimize Reporting 
 */

// Load Inquiries and items where total1 has not been calculated

use Fox\Html\Interval;
use Fox\Html\Input;
use Fox\Html\Screen;
use Fox\Dao\SqlFragment;
use Fox\SalesDistribution\Sales\Inquiry;

$screen = new Screen('e1','execute');
$block = new Fox\Html\Block('b1','Update Totalx Qty in inquiries');
$block->add(new Interval("start_date"));
$block->add(new Input("dbcnt"));
$screen->add($block);
if($screen->submitted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		$finder = new Inquiry();
		$fragments[] = new SqlFragment("amount_initial > 0 ");
		$fragments[] = $block->item("start_date")->fragments();
		$res = $finder->find(SqlFragment::combine($fragments),null,null,$inputs['dbcnt']);
		
		Fox\Message::Information("Inquiry Loaded : ".count($res));
		foreach($res as $inquiry)
		{
			try {
				
				$inquiry->amounts(true);
				$inquiry->prepareUpdate(Inquiry::CHANGE_DEFAULT);
				if($inquiry->update())
				{
					Fox\Message::Information("Inquiry $inquiry->reference has been updated");
				}
			}
			catch (Fox\Object\Exception\Save $e)
			{
				Fox\Core\Errors::pushMessage($e);
			}
			
		}
	}
}
echo $screen->display();